//
//  NetWorkTool.m
//  项目唯一
//
//  Created by 谌桃 on 16/7/6.
//  Copyright © 2016年 ryan.chen. All rights reserved.
//

#import "NetWorkTool.h"

@implementation NetWorkTool

#pragma 网络单例
+ (instancetype)sharedManager{
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    static NetWorkTool * instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] initWithSessionConfiguration:configuration];
        
        //可接受的响应类型
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
        
        //请求类型
        instance.requestSerializer = [AFJSONRequestSerializer serializer];
        
        //设置请求 超时时长
        instance.requestSerializer.timeoutInterval = 10.0;
    });
    
    return instance;
}

#pragma TODO
//- (BOOL)isReachableByAFN{
//
//    return
//}


//GET  请求
- (void)GETRequestWithUrlString:(NSString *)urlString params:(NSDictionary *)params successBlock:(SuccessCompletedBlock)success failtureBlock:(FailureCompletedBlock)failture{

    [[NetWorkTool sharedManager] GET:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        failture(error);
    }];
}

//POST 请求
- (void)POSTRequestWithUrlString:(NSString *)urlString params:(NSDictionary *)params successBlock:(SuccessCompletedBlock)success failtureBlock:(FailureCompletedBlock)failture{

    [[NetWorkTool sharedManager] POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failture(error);
    }];
}

@end
